/*xcy7TAC9s06TtVxkio9q+OzgxSPr9OVDG6zlgW1svtpTFEvzFWA6Za5NDEkHQGAI*/
/*
 * @Descripttion: 供应商删除
 * @version:
 * @Author: Chendch
 * @Date: 2021-01-03 15:05:53
 * @LastEditors: Chendch
 * @LastEditTime: 2021-04-09 11:04:39
 */
import {ajax, toast, cardCache } from "nc-lightapp-front";
import { getCurParam  } from './SupplierUtils';
import {Urls} from "../main/CONSTANCE";
import {dataSource} from "../card/CONSTANCE";
const { getNextId } = cardCache;
import {setCardButtonStatus} from './../card/events/clickEvent/SupCardButtonAction';
/**
    * 主列表 操作列删除 弹窗回调事件
    * @param isAsyncDel
*/
function onMainTableOperateDelClick(isAsyncDel) {
    let param = getCurParam.call(this,'pk_supplier');
    let nextPk = this.state.openCard?this.props.cardPagination.getNextCardPaginationId ({id:param.pk_supplier[0],status:'1'}):null;
    doDelete.call(this, {...param,isAsyncDel:isAsyncDel,nodeType:this.props.nodeType,pk_curOrg:this.state.envParam.pk_org},
        isAsyncDel?()=>{
            deleteTableRecordByIndex.call(this, param);
            //this.afterDeletePrompt(isAsyncDel);
        }:null,(res)=>{
            if(res.success){
                deleteAfterEvent.call(this, {...param,nextPk:nextPk},isAsyncDel?()=>{}:deleteTableRecordByIndex.bind(this));//删除后事件
                !isAsyncDel && afterDeletePrompt.call(this, isAsyncDel);
            }
        }
    );
}
function doDelete(param,beforeCallback,afterCallback) {
    beforeCallback && beforeCallback();
    ajax({
        url:Urls.delSupplier,
        data:param,
        success:(res)=>{
            afterCallback && afterCallback(res);
        }
    })
}
function afterDeletePrompt(isAsyncDel) {
    if(this&&this.DeleteDialog){
        this.DeleteDialog.closeDialog();//关弹窗
    }
    this.setState({openDeleteDialog:false},()=>{
        ///* 国际化处理： 数据已停用并执行后台删除任务，稍后请在删除查询方案中查询结果！*/ or /* 国际化处理： 删除成功！*/
        toast({ color: 'success', content: isAsyncDel?(this.Lang['10140SUG-000148']+this.Lang['10140SUG-000149']):this.Lang['10140SUG-000073']});
    });
}
/**
 * 删除表格行
 * @param param
 */
function deleteTableRecordByIndex(param) {
    param.pk_supplier && param.pk_supplier.forEach(pk=>{
        this.props.cardPagination.setCardPaginationId({id:pk,status:'3'});
    })
    param.indexs && setTimeout(()=>{
        // let newIndexs = param.indexs.reverse();
        param.indexs.forEach(index=>{//迭代按index删除record
            this.state.table && this.props.table.deleteTableRowsByIndex(this.state.table.mainTable.id,index);
        });
    },0);
}
/**
 * 删除后事件
 * @param param
 * update by yufwm 2020/03/13 后台删除 弹窗关闭
 * update by yufwm 2021-03-13 删除后查询列表数据 不提示查询信息
 * update by chendch 2021-02-25 删除后新增的仅保留新增按钮
 */
function deleteAfterEvent(param,deleteTableRecordByIndex) {
    //在列表上删除行
    // deleteTableRecordByIndex && deleteTableRecordByIndex(param);
    let mainTableData = {rows:this.state.queryRecords}
    let tableData = mainTableData || this.props.table.getAllTableData(this.state.table.mainTable.id);
    if(this.state.openCard){
        let record = null;
        if(!!tableData && tableData.rows.length>0 && param.nextPk){
            record = tableData.rows.find(row=>{if(param.nextPk == row.values['pk_supplier'].value){return row}else{return tableData[tableData.rows.length-1];}});
        }
        let id = this.props.getUrlParam('id'),
            nextId = getNextId(id, dataSource),
            {card} = this.state;
        //在卡片上 需要重新加载卡片信息
        this.setState({selectedRecords:record?[record]:null,dirtyRecords:record?[record]:null, relateQueryId: nextId},()=>{
            if(!nextId) {//新增删除--仅有一条数据，清空数据，只显示新增按钮
                this.props.button.setButtonVisible(
                    ['Card_Save','Card_Cancel','Card_Edit','Card_Refresh','Card_Delete','Card_Copy',
                        'Card_BatchUpdate','Card_Assign',
                        'Card_BankAccount','Card_AddressBook','Card_SupRename','Card_Approve','Card_UnApprove',
                        'Card_Freeze','Card_AssistFun','Card_Associate',
                        'Card_OrgBrowse','Card_BrowseOrgDoc','Card_Print','Card_SaveAdd','Card_Enable','Card_Disable','Card_ApplyQuery','More','Card_Supplier_Template'],false);
                this.props.button.setButtonVisible(['Card_Add'],true);
                this.props.cardPagination.setCardPaginationVisible('cardPaginationBtn', false);
                this.props.form.EmptyAllFormValue(card.form.formId);
                this.props.form.EmptyAllFormValue(card.form.baseFormId);
                this.props.editTable.setTableData("suplinkman", {rows: []});
                this.props.editTable.setTableData("supbankacc", {rows: []});
                this.props.editTable.setTableData("supcountrytaxes", {rows: []});
            }else {
                this.initPageData(this.setCardValue,this.setCardStatus,null,null,null,!record);
                !record && setCardButtonStatus.call(this,'edit_cancel',true);
                !record && this.props.button.setButtonVisible({
                    Card_Enable:false,
                    Card_Disable:false,
                    Card_Approve:false,
                    Card_UnApprove:false
                });
            }
        })
    }else{
        // 删除后查询列表数据 不提示查询信息
        this.onSearch({checagepage:true},this.setTableData);
    }
    // 后台删除 弹窗关闭
    this.setState({openDeleteDialog:false});
}

export {
    onMainTableOperateDelClick
};
/*xcy7TAC9s06TtVxkio9q+OzgxSPr9OVDG6zlgW1svtpTFEvzFWA6Za5NDEkHQGAI*/
